# 引入Django的HttpResponse包依赖
from django.http import HttpResponse
import json
# 引入service包中的user_services功能
from student.service import user_services
from student.service import student_services
from student.service import course_services
from student.service import score_services


# 管理员登录功能
# HTTP方法：post
# body: json格式，包含username,password参数
def login(request):
    body = json.loads(request.body)
    # 调用user_services.login函数，加入content_type参数保证返回内容为json格式
    return HttpResponse(user_services.login(body), content_type="application/json")


# 管理员登出功能
# HTTP方法:get
# request中包含COOKIE['token']
def logout(request):
    token = request.COOKIES['token']
    return HttpResponse(user_services.logout(token), content_type="application/json")


# 管理员注册
def register(request):
    body = json.loads(request.body)
    return HttpResponse(user_services.register(body), content_type="application/json")


# 分页查询学生信息
# HTTP方法: post
# body: json格式，包含page, content参数
def query_student(request):
    body = json.loads(request.body)
    return HttpResponse(student_services.query_student(body), content_type="application/json")


# 插入学生信息
# HTTP方法: post
# body: json格式，包含stu_num, stu_name, stu_birth, stu_add参数
def insert_student(request):
    body = json.loads(request.body)
    return HttpResponse(student_services.insert_student(body), content_type="application/json")


# 更新学生信息
# HTTP方法: post
# body: json格式，包含id, stu_num, stu_name, stu_birth, stu_add参数
def update_student(request):
    student_body = json.loads(request.body)
    return HttpResponse(student_services.update_student(student_body), content_type="application/json")


# 删除学生信息
# HTTP方法: get
# id 为必须选项
def remove_student(request):
    id = request.GET.get('id')
    return HttpResponse(student_services.remove_student(id), content_type="application/json")


# 分页查询课程信息信息
def query_course(request):
    page = request.GET.get('page', 1)  # 获取request传入内容，如果没有则设置默认值1
    return HttpResponse(course_services.query_courses(page), content_type="application/json")


def query_all_course(request):
    return HttpResponse(course_services.query_all_courses(), content_type="application/json")


# 插入课程信息
def insert_course(request):
    body = json.loads(request.body)
    return HttpResponse(course_services.insert_course(body), content_type="application/json")


# 更新课程信息
def update_course(request):
    student_body = json.loads(request.body)
    return HttpResponse(course_services.update_course(student_body), content_type="application/json")


# 删除课程信息
def remove_course(request):
    id = request.GET.get('id')
    return HttpResponse(course_services.remove_course(id), content_type="application/json")


# 查询所有新生选择课程
def score_query(request):
    body = json.loads(request.body)
    return HttpResponse(score_services.query_student_score(body), content_type="application/json")


# 绑定学生和课程(添加score表)
# 输入内容包括学生学号stu_num和课程编号c_id
def score_add(request):
    bind_body = json.loads(request.body)
    return HttpResponse(score_services.bind_student_course(bind_body))


# 解绑学生和课程(删除score表)
# 输入内容包括score表id
def score_delete(request):
    s_id = request.GET.get('id')
    return HttpResponse(score_services.unbind_student_course(s_id))


# 更新学生成绩
# 输入内容包括: score表id和成绩score
def score_update(request):
    score_dict = json.loads(request.body)
    return HttpResponse(score_services.update_student_score(score_dict))


# 验证token功能
# HTTP方法：get
# request中包含COOKIE['token']
def get_by_token(request):
    token = ''
    # 判断http request请求的cookies是否有token内容，如果有就覆盖
    if 'token' in request.COOKIES.keys():
        token = request.COOKIES['token']
    return HttpResponse(user_services.get_token_info(token))

